package com.baosight.risk.base.service;

import com.baosight.risk.base.entity.BaseEntity;
import com.baosight.risk.base.mapper.BaseMapper;
import com.baosight.risk.common.utils.ShiroUtils;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import com.baosight.risk.web.page.PageDomain;
import com.baosight.risk.web.page.TableSupport;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

/**
 * @param <T>
 * @author duanyongqiang
 * <p>
 * 基础service实现
 */
public class BaseServiceImpl<T extends BaseEntity> implements BaseService<T> {

    @Autowired
    private ApplicationContext applicationContext;

    private BaseMapper baseMapper;

    private Class<T> entityClass;

    public BaseServiceImpl() {
        //获取当前泛型类
        Type type = this.getClass().getGenericSuperclass();
        if (type.toString().indexOf("BaseService") != -1) {
            ParameterizedType type1 = (ParameterizedType) type;
            Type[] types = type1.getActualTypeArguments();
            entityClass = (Class<T>) types[0];
        } else {
            type = ((Class<T>) type).getGenericSuperclass();
            ParameterizedType type1 = (ParameterizedType) type;
            Type[] types = type1.getActualTypeArguments();
            entityClass = (Class<T>) types[0];
        }
    }

    /**
     * 获取当前登陆用户名称
     *
     * @return
     */
    public XtzyUser getUser() {
        return ShiroUtils.getUser();
    }


    @Override
    public BaseMapper getMapper() {
        if (baseMapper == null) {
            String name = entityClass.getSimpleName() + "Mapper";
            String first = String.valueOf(name.charAt(0));
            name = name.replaceFirst(first, first.toLowerCase());
            baseMapper = applicationContext.getBean(name, BaseMapper.class);
        }
        return baseMapper;
    }

    @Override
    public T get(String id) {
        return (T) getMapper().selectByPrimaryKey(id);
    }

    @Override
    public List select(BaseEntity entity) {
        return getMapper().select(entity);
    }

    @Override
    public List selectAll() {
        return getMapper().selectAll();
    }

    @Override
    public Integer insert(BaseEntity entity) {
        return getMapper().insert(entity);
    }

    @Override
    public Integer update(BaseEntity entity) {
        return getMapper().updateByPrimaryKeySelective(entity);
    }

    @Override
    public Integer delete(String id) {
        return getMapper().deleteByPrimaryKey(id);
    }

    /**
     * 设置请求分页数据
     */
    protected void servicePage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageIndex = pageDomain.getPageIndex();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageIndex) && StringUtils.isNotNull(pageSize)) {
            String orderBy = pageDomain.getSortField();
            PageHelper.startPage(pageIndex, pageSize, orderBy);
        }
    }
}
